<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Dealing with XML errors</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="simplexml.examples-basic.html">« Basic SimpleXML usage</a></li>
      <li style="float: right;"><a href="class.simplexmlelement.html">SimpleXMLElement »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="simplexml.examples.html">范例</a></li>
    <li>Dealing with XML errors</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="simplexml.examples-errors" class="section">
  <h2 class="title">Dealing with XML errors</h2>
  <p class="para">
   Dealing with XML errors when loading documents is a very simple task.
   Using the <a href="book.libxml.html" class="link">libxml</a> functionality it is
   possible to suppress all XML errors when loading the document and then
   iterate over the errors.
  </p>
  <p class="para">
   The <span class="classname"><a href="class.libxmlerror.html" class="classname">libXMLError</a></span> object, returned by
   <span class="function"><a href="function.libxml-get-errors.html" class="function">libxml_get_errors()</a></span>, contains several properties
   including the <a href="class.libxmlerror.html#libxmlerror.props.message" class="link">message</a>,
   <a href="class.libxmlerror.html#libxmlerror.props.line" class="link">line</a> and
   <a href="class.libxmlerror.html#libxmlerror.props.column" class="link">column</a> (position) of the
   error.
  </p>
  <p class="para">
   <div class="example" id="example-5448">
    <p><strong>示例 #1 Loading broken XML string</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />libxml_use_internal_errors</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sxe&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">simplexml_load_string</span><span style="color: #007700">(</span><span style="color: #DD0000">"&lt;?xml&nbsp;version='1.0'&gt;&lt;broken&gt;&lt;xml&gt;&lt;/broken&gt;"</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$sxe&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Failed&nbsp;loading&nbsp;XML\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach(</span><span style="color: #0000BB">libxml_get_errors</span><span style="color: #007700">()&nbsp;as&nbsp;</span><span style="color: #0000BB">$error</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\t"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$error</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">message</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Failed loading XML
    Blank needed here
    parsing XML declaration: &#039;?&gt;&#039; expected
    Opening and ending tag mismatch: xml line 1 and broken
    Premature end of data in tag broken line 1
</pre></div>
    </div>
   </div>
  </p>
  <div class="section">
   <h2 class="title">参见</h2>
   <p class="para">
    <ul class="simplelist">
     <li class="member"><span class="function"><a href="function.libxml-use-internal-errors.html" class="function">libxml_use_internal_errors()</a></span></li>
     <li class="member"><span class="function"><a href="function.libxml-get-errors.html" class="function">libxml_get_errors()</a></span></li>
     <li class="member"><a href="class.libxmlerror.html" class="xref">libXMLError</a></li>
    </ul>
   </p>
  </div>
 </div></div></div></body></html>